/*
 * Copyright 2006 - 2007 The MITRE Corporation. All rights reserved.
 * Written by Ayal Spitz
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 2.1
 * (the "License"); you may not use this file except in compliance
 * with the License and the following two conditions:
 * 
 * The US Government will not be charged any license fee and/or royalties 
 * related to this software.
 * 
 * Neither name of The MITRE Corporation; nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * You may obtain a copy of the License at
 *
 *     http://www.gnu.org/copyleft/lesser.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.mitre.rome;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedOutput;

public class SyndEntryOutput{
	private SyndFeedOutput			syndFeedOutput = null;
	private XMLOutputter			xmlOutputter = null;
	
	public SyndEntryOutput(){
		syndFeedOutput = new SyndFeedOutput();

		Format				compactFormat = null;

		compactFormat = Format.getCompactFormat();
		compactFormat.setOmitEncoding(true);	
		xmlOutputter = new XMLOutputter(compactFormat);
	}
	
	@SuppressWarnings("unchecked")
	public List<Element> outputJDOM(SyndFeed feed) throws FeedException{
		Document			doc = null;
			Element				element = null;
		List<Element>		elementList = null;
		
		doc = syndFeedOutput.outputJDom(feed);
		element = doc.getRootElement();
		element = element.getChild("channel");
		elementList = ((List<Element>)(element.getChildren("item")));

		return elementList;
	}
	
	public List<String> outputString(SyndFeed feed) throws FeedException{
		return outputString(outputJDOM(feed));
	}
	
	protected List<String> outputString(List<Element> elementList){
		List<String>		stringList = null;
		
		stringList = new ArrayList<String>(elementList.size());
		
		for(Element element:elementList){
			stringList.add( xmlOutputter.outputString(element) );
		}
		
		return stringList;
	}
	
	@SuppressWarnings("unchecked")
	public Set<MetaEntry> outputMetaEntry(SyndFeed feed) throws FeedException{
		List<SyndEntry>			syndEntryList = null;
		List<Element>			elementList = null;
		List<String>			stringList = null;
		int						size = 0;
		Set<MetaEntry>			entrySet = null;
		MetaEntry				entry = null;
		
		syndEntryList = feed.getEntries();
		elementList = outputJDOM(feed);
		stringList = outputString(elementList);
		
		size = syndEntryList.size();
		entrySet = new HashSet<MetaEntry>(size);
		
		for(int i=0;i<size;i++){
			entry = new MetaEntry(syndEntryList.get(i),elementList.get(i),stringList.get(i));
			entrySet.add(entry);
		}
		
		return entrySet;
	}
	
	public class MetaEntry{
		private SyndEntry		entry = null;
		private String			str = null;
		private Element			element = null;
		
		private MetaEntry(SyndEntry entry,Element element,String str){
			this.entry = entry;
			this.str = str;
			this.element = element;
		}
		
		public SyndEntry getEntry(){ return entry; }
		public String getString(){ return str; }
		public Element getElement(){ return element; }

		@Override
		public boolean equals(Object obj){
			if (obj instanceof MetaEntry){
				return entry.equals(((MetaEntry)obj).getEntry());
			} else {
				return false;
			}
		}

		@Override
		public int hashCode(){ return entry.hashCode(); }
		
		public String toString(){ return entry.getTitle(); }
	}
}
